#!/usr/bin/env ruby
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# frozen_string_literal: true

require 'set'
require_relative '../defconfig.rb'

names = Set.new %w[
  ES_USER
  ES_PASSWORD
  lab
]

defaults = relevant_defaults(names)

service_authentication = relevant_service_authentication(names)
env = docker_env(service_authentication)

DOCKER_CCI = '/c/compass-ci'
DOCKER_LKP = '/c/lkp-tests'
docker_rm 'auto-submit'

cmd = %w[
  docker run
  --restart=always
  --name auto-submit
] + env + %W[
  -u 1090:1090
  -d
  -e CCI_SRC=#{DOCKER_CCI}
  -e LKP_SRC=#{DOCKER_LKP}
  -e CCI_REPOS=/c
  -v #{ENV['CCI_SRC']}:#{DOCKER_CCI}
  -v #{ENV['LKP_SRC']}:#{DOCKER_LKP}
  -v /c/lab-#{defaults['lab']}:/c/lab-#{defaults['lab']}
  -v /etc/localtime:/etc/localtime:ro
  -v /etc/compass-ci/defaults:/etc/compass-ci/defaults:ro
  -v /etc/compass-ci/register:/etc/compass-ci/register:ro
  -v /srv/git:/srv/git
  -w /c/compass-ci/sbin
  alpine:auto-submit
]

cmd += ['sh', '-c', 'umask 002 && ./auto_submit']
system(*cmd)
